﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace QGZX.Logic.Admin
{
    /// <summary>
    /// 管理员登陆的相关业务
    /// </summary>
    public class AdminController : Controller
    {

        #region 管理员 登陆业务 +  ActionResult Login()
        /// <summary>
        /// 管理员 登陆业务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Skip]
        public ActionResult Login()
        {
            return View();
        }
        #endregion

        #region 管理员 登陆业务 +  ActionResult Login()
        /// <summary>
        /// 管理员 登陆业务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Skip]
        public ActionResult Login(MODEL.ViewModel.LoginAdmin adminInfo)
        {
            //服务器端验证
            if (!ModelState.IsValid)
            {
                return OperateContext.Current.RedirectAjax("err", "没有权限", null, "");
            }
            if (OperateContext.Current.LoginAdmin(adminInfo))
            {
                return OperateContext.Current.RedirectAjax("ok", "登陆成功", null, "/admin/admin/index");
            }
            else
            {
                return OperateContext.Current.RedirectAjax("err", "登陆失败", null, "");
            }
        }
        #endregion

        #region 首页 +ActionResult Index()
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            return View();
        }   
        #endregion

        #region 3.0 根据当前登陆用户 权限 生成菜单 +GetMenuData()
        /// <summary>
        /// 根据当前登陆用户 权限 生成菜单
        /// </summary>
        /// <returns></returns>
        [AjaxRequest]
        public ActionResult GetMenuData()
        {
            //MODEL.EasyUIModel.Tree tree = Helper.OperateContext.CurUserPermission;
            return Content(OperateContext.Current.UsrTreeJsonStr);
            //return Content("{\"data\":[{\"id\":1,\"text\":\"后台菜单导航\",\"state\":\"open\",\"attributes\":null,\"children\":[{\"id\":2,\"text\":\"系统管理\",\"Checked\":false}]}]}");
        }
        
        
        #endregion

        #region 管理员列表视图 + ActionResult Admin()
        /// <summary>
        /// 管理员列表视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Admin()
        {
            return View();
        } 
        #endregion

        #region 获取管理员数据 +GetAdminData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取管理员数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAdminData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_adminBLL.GetPagedList(pageIndex, pageSize, x => x.Id != null, m => m.Id).Select(x => x.ToPOCO());

            int rowCount = OperateContext.Current.BLLSession.Iqgzx_adminBLL.GetListBy(x => x.Id != null).Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改管理员 window 的html + EditAdmin()
        /// <summary>
        /// 加载修改管理员 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditAdmin(int id)
        {
            SetDropDonwList();
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_adminBLL.GetListBy(x => x.Id == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改管理员信息 EditAdmin(MODEL.qgzx_admin model)
        /// <summary>
        ///post修改管理员信息 EditAdmin(MODEL.qgzx_admin model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditAdmin(MODEL.qgzx_admin model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_adminBLL.Modify(model, "LoginName", "Number", "Password", "Remark", "DepartmentId");
            if (res > 0)
            {
                return Redirect("/admin/admin/admin");
            }
            return Redirect("/admin/admin/admin");
        }
        #endregion

        #region 在这新增管理员 html代码 + AddAdmin()
        /// <summary>
        /// 在这新增管理员 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddAdmin()
        {
            SetDropDonwList();
            return PartialView("EditAdmin");
        }
        #endregion

        #region 在这新增管理员 html代码 + AddAdmin(MODEL.qgzx_admin model)
        /// <summary>
        /// 在这新增管理员 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddAdmin(MODEL.qgzx_admin model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_adminBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/admin/admin/admin?ok");
            }
            return Redirect("/admin/admin/admin?err");
        }
        #endregion

        #region 删除管理员 + DelAdmin()
        /// <summary>
        /// 删除管理员 + DelAdmin()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelAdmin()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_adminBLL.DelBy(x => x.Id == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限", null, "");
            }
        }
        #endregion

        //-----------------------------------------------------
        #region 设置新增和修改通用的下拉框数据 + SetDropDonwList()
        /// <summary>
        /// 设置新增和修改通用的下拉框数据
        /// </summary>
        void SetDropDonwList()
        {
            /*
             管理员对应的部门（角色）id
             */
            List<MODEL.qgzx_department> list = OperateContext.Current.BLLSession.Iqgzx_departmentBLL.GetListBy(x => x.Id != null);
            List<SelectListItem> selectList = new List<SelectListItem>();
            foreach (MODEL.qgzx_department model in list)
            {
                selectList.Add(new SelectListItem() { Text = model.Name, Value = model.Id.ToString() });
            }
            ViewBag.departmentList = selectList;
        } 
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string LoginName = Request.QueryString["LoginName"];
            string Number = Request.QueryString["Number"];
            string Remark = Request.QueryString["Remark"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_adminBLL.GetListBy(x => x.Id != null).ToList().Select(x=>x.ToPOCO());
            if (LoginName != "")
            {
                list = list.Where(x => x.LoginName.Contains(LoginName)).ToList();
            }
            if (Number != "")
            {
                list = list.Where(x => x.Number.Contains(Number)).ToList();
            }
            if (Remark != "")
            {
                list = list.Where(x => x.Remark.Contains(Remark)).ToList();
            }
            
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion
    }
}
